import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import '../../../app/network/http_client.dart';
import '../../../app/data/model/article_model.dart';

class ArticleDetailController extends GetxController {
  // 可观察单篇文章
  var article = ArticleModel(
    id: 0,
    title: '',
    summary: '',
    content: '',
    date: DateTime.now(),
  ).obs;

  // 加载标志
  var isLoading = true.obs;

  @override
  void onInit() {
    super.onInit();
    debugPrint('Get.arguments = ${Get.arguments}'); // ← 看这里
    final int id = Get.arguments as int; // 列表页传过来的 id
    loadArticle(id);
  }

  Future<void> loadArticle(int id) async {
    try {
      isLoading.value = true;
      debugPrint('开始请求 /article/$id'); // ← 新增
      final raw = await HttpClient.get('/article/$id', ArticleModel.fromJson);
      debugPrint('拿到原始数据：$raw');
      article.value = raw.first; // 接口只返回一条
    } catch (e) {
      debugPrint('错误详情：$e');
      Get.snackbar('错误', '加载详情失败：$e');
    } finally {
      isLoading.value = false;
    }
  }
}
